//
//  ForgetVC.m
//  Running
//
//  Created by 郑东喜 on 2016/10/30.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

#import "ForgetVC.h"
#import "LoginVC.h"

#import "Encryption.h"

#import "MBProgressHUD.h"
#import "MBManager.h"

@interface ForgetVC ()<CountDownDelegate,UITextFieldDelegate,CAAnimationDelegate,CommonToolDelegate>

@end

@implementation ForgetVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    
    //读取上一次的用户名
    
    if ([stan objectForKey:forgetUName] != nil) {
        self.tfNum.text = [stan objectForKey:forgetUName];
    }

    [self.tfNum setTag:300];
    [self.tfAuto setTag:301];
    [self.tfpass setTag:302];
}

#pragma mark - 设置uitextfieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    switch (textField.tag) {
        case 300:
            [self.tfNum resignFirstResponder];
            [self.tfAuto becomeFirstResponder];
            break;
            
        case 301:
            [self.tfAuto resignFirstResponder];
            [self.tfpass becomeFirstResponder];
            break;
            
        case 302:
            [self.tfpass resignFirstResponder];
            break;
            
        default:
            break;
    }
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    //保存当前用户名到本地
    [stan setObject:self.tfNum.text forKey:forgetUName];
    [stan synchronize];
}

- (CountDown *)cBtn {
    if (!_cBtn) {
        _cBtn = [[CountDown alloc] init];
        
        _cBtn.delegate = self;
        //开始倒计时
        [_cBtn openCountdown];
    }
    return _cBtn;
}

- (IBAction)sendAuto:(CountDown *)sender {
    
    [self.tools setDelegate:self];
    [self.tools sendAutoSEL:self.tfNum];
    
}

- (IBAction)changeSEL:(id)sender {
    [self.tools forgetPassSELWith:self.tfNum withTFpass:self.tfpass withAutoTF:self.tfAuto];
    self.tools.delegate = self;
}

- (IBAction)returnSEL:(id)sender {

    
    NSLog(@"%s  %d  ",__func__,__LINE__);
 
    [self returnVC:[LoginVC new]];
}

- (void)changeImgStatus:(CountDown *)countDonw {
    NSLog(@"%s  %d  %@",__func__,__LINE__,@"忘记密码事件");
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //主线程执行代码
        [self.sendAutoImg setImage:[UIImage imageNamed:@"btn_yzm02"]];
    });
}

#pragma mark - 代理回调
- (void)finishgetPassword:(CommonTool *)getPasswordSuccess {
    NSLog(@"%s  %d  ",__func__,__LINE__);
   [self returnVC:[LoginVC new]];
}

- (void)finishSendMsg:(CommonTool *)sendMsgSuccess {
    [self cBtn];
    [self.sendAutoImg setImage:[UIImage imageNamed:@"sendSuccess"]];
}

@end
